package cn.itrip.auth.controller;

import cn.itrip.auth.service.TokenService;
import cn.itrip.auth.service.UserService;
import cn.itrip.beans.dto.Dto;
import cn.itrip.beans.pojo.ItripUser;
import cn.itrip.beans.vo.ItripWechatTokenVO;
import cn.itrip.common.DtoUtil;
import cn.itrip.common.ErrorCode;
import cn.itrip.common.UrlUtils;
import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

/**
 * 第三方登录控制器
 * @author
 *
 */
@Controller
@RequestMapping(value = "/vendors")
public class VendorsController {

    @Resource
    private UserService userService;
    @Resource
    private TokenService tokenService;

    /**
     * 微信登录——第一步：获取code
     * @param response
     */
    @RequestMapping(value = "/wechat/login")
    public void wechatLogin(HttpServletResponse response){
        String qrconnect="https://open.weixin.qq.com/connect/qrconnect?appid=wx3e9342f14cd7e34f&redirect_uri=http%3a%2f%2fitripfc.free.idcfengye.com%2fitrip-auth%2fvendors%2fwechat%2fcallback&response_type=code&scope=snsapi_login&state=STATE#wechat_redirect";
        try {
            response.sendRedirect(qrconnect);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /**
     * 微信登录——第二步：通过code换取access_token
     * @param code
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping(value = "/wechat/callback")
    public void wechatCallback(@RequestParam String code, HttpServletRequest request, HttpServletResponse response){
        try {
            if (null != code){
                String accessTokenUrl="https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx3e9342f14cd7e34f&secret=f30f8137a7a90d9e4c80530c920664e1&code="+
                        code+"&grant_type=authorization_code";
                response.setContentType("text/html;charset=utf-8");
                String json=UrlUtils.loadURL(accessTokenUrl);
                Map<String,Object> wechatToken=JSON.parseObject(json, Map.class);

                //验证本地库是否存在该用户
                ItripUser user=userService.findByUserCode(wechatToken.get("openid").toString());
                if(user==null){//如果不存在则添加用户
                    user=new ItripUser();
                    user.setUserCode(wechatToken.get("openid").toString());//微信用户openid作为本地库用户名
                    user.setUserType(1);
    //				user.setFlatID(wechatToken.getOpenid());
                    user.setUserName(wechatToken.get("openid").toString());
                    user.setCreationDate(new Date());
                    user.setActivated(1);

                    userService.itriptxCreateUser(user);
                }
                String token = tokenService.generateToken(
                        request.getHeader("user-agent"), user);
                tokenService.save(token, user);
                /*
                response.setHeader("token", token);
                response.setHeader("access_token", wechatToken.get("access_token").toString());
                response.setHeader("expires_in", wechatToken.get("expires_in").toString());
                response.setHeader("refresh_token", wechatToken.get("refresh_token").toString());
                response.setHeader("openid", wechatToken.get("openid").toString());
                */


                //返回前端处理
                StringBuilder loginPage=new StringBuilder();
                loginPage.append("http://itrip.test.com/#/login");
                loginPage.append("?user_type=1&token="+token);
                loginPage.append("&access_token="+wechatToken.get("access_token").toString());
                loginPage.append("&expires_in="+wechatToken.get("expires_in").toString());
                loginPage.append("&refresh_token="+wechatToken.get("refresh_token").toString());
                loginPage.append("&openid="+wechatToken.get("openid").toString());
                response.sendRedirect(loginPage.toString());
            } else {
                response.sendRedirect("http://itrip.test.com/#/login");
            }

        } catch (Exception e1) {
            e1.printStackTrace();
        }
    }

    /**
     * 获取微信用户信息
     * @param accessToken 微信会话凭据
     * @param openid 微信用户唯一标识
     * @return
     */
    @RequestMapping(value = "/wechat/user/info", method = RequestMethod.GET,produces= "application/json")
    @ResponseBody
    public Dto wechatUserInfo(
            @RequestParam String accessToken,
            @RequestParam String openid){
        try {
            //加载用户信息
            String userInfoJson=UrlUtils.loadURL("https://api.weixin.qq.com/sns/userinfo?access_token="
                    +accessToken
                    +"&openid="+ openid
            );
            Map<String,Object> userInfo=JSON.parseObject(userInfoJson, Map.class);

            return DtoUtil.returnDataSuccess(userInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail(e.getMessage(), ErrorCode.AUTH_UNKNOWN);
        }
    }
    /**
     * 会话保持
     * 1、保持和itrip的会话
     * 2、保持和wechat的会话
     * @return
     */
    @RequestMapping(value = "/wechat/token/refresh", method = RequestMethod.POST,produces= "application/json")
    @ResponseBody
    public Dto wechatRefreshToken(HttpServletRequest request,HttpServletResponse response){

        String agent=request.getHeader("user-agent");
        String token=request.getHeader("token");
        String refreshToken=request.getHeader("refreshtoken");//此处header中的名称使用“_”不能获取

        try {
            //1、保持和itrip的会话
            String newToken=tokenService.reloadToken(agent, token);
            //2、保持和wechat的会话
            String refreshTokenUrl="https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=wx9168f76f000a0d4c&grant_type=refresh_token&refresh_token="+refreshToken.trim();

            String json=UrlUtils.loadURL(refreshTokenUrl);
            Map<String,Object> wechatToken=JSON.parseObject(json, Map.class);
            if(null!=wechatToken.get("errcode")){
                return DtoUtil.returnFail(wechatToken.get("errmsg").toString(), ErrorCode.AUTH_REPLACEMENT_FAILED);
            }

            //返回ItripWechatTokenVO (整合了本地会话与微信会话)
            ItripWechatTokenVO wechatTokenVO=new ItripWechatTokenVO(newToken,
                    Calendar.getInstance().getTimeInMillis()+TokenService.SESSION_TIMEOUT*1000,//2h有效期
                    Calendar.getInstance().getTimeInMillis());
            wechatTokenVO.setAccessToken(wechatToken.get("access_token").toString());
            wechatTokenVO.setExpiresIn(wechatToken.get("expires_in").toString());
            wechatTokenVO.setRefreshToken(wechatToken.get("refresh_token").toString());
            wechatTokenVO.setOpenid(wechatToken.get("openid").toString());
            return DtoUtil.returnDataSuccess(wechatTokenVO);

        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail(e.getMessage(), ErrorCode.AUTH_REPLACEMENT_FAILED);
        }

    }

}
